package cn.libinal.scloud.base.oauth.security;

import cn.libinal.scloud.base.common.dto.ClientDto;
import cn.libinal.scloud.base.common.web.ResultJson;
import cn.libinal.scloud.base.oauth.service.BaseClients;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;

/**
 * 定义客户端Service
 * @author libin
 */
@Slf4j
@Component
public class ClientDetailsServiceImpl implements ClientDetailsService {

    private final BaseClients baseClients;

    public ClientDetailsServiceImpl(BaseClients baseClients) {
        this.baseClients = baseClients;
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        ResultJson<ClientDto> resultJson = baseClients.findClientByClientId(clientId);

        if (resultJson.getStatus() == 0) {
            throw new ClientRegistrationException("客户端不存在");
        }
        return new CustomClientDetails(resultJson.getData());
    }
}
